// pages/addrSelect/addrSelect.js
var $ = require('../util/commit.js');
var TXMap = $.newTXmap();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Midlatitude: '',
    Midlongitude: '',
    Mlatitude: '',
    Mlongitude: '',
    AddressList:[],
    AddFindList:[],
    addFindVal:'',
    addressVal:'',
    selectIndexVal:-1,
    inputFocusStatus:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getLocation();
  },
  // 获取当前人地理位置
  getLocation: function (e) {
    let that = this;

    wx.getLocation({
      type: 'wgs84',
      success(res) {
        that.setData({
          Midlatitude: res.latitude,
          Midlongitude: res.longitude,
          Mlatitude: res.latitude,
          Mlongitude: res.longitude,
        });
      //  let  location = res.latitude + ',' + res.longitude

        that.reverseGeocoder(res.latitude, res.longitude)
        
      }
    })
  },
  // reverseGeocoder坐标到坐标所在位置的文字描述的转换
  reverseGeocoder: function (latitude,longitude){
    let that=this;
    TXMap.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      page_size: 20,
      get_poi:1,
      success: function (res) {
     
        let location={
          latitude: latitude,
          longitude: longitude
        };
        console.log(location);
        console.log(res);
        that.setData({
          AddressList: res.result.pois
        })
        // that.addFindList(res.result.address_component.street_number, location);
      },
      fail: function (res) {
        console.log(res);
        
      },
      complete: function (res) {
       
      }
    })
  },
  // 过滤当前点的周围
  addFindList: function (resultKeyWord, location){
    console.log(resultKeyWord);
    console.log(location);
    let that=this;
    TXMap.search({
      keyword: resultKeyWord,  //搜索关键词
      location: location,  //设置周边搜索中心点
      address_format:'short',
      page_size:20,
      success: function (res) { //搜索成功后的回调
      console.log(res);
        that.setData({
          AddressList:res.data
        })
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
  
      }
    })
  },
  // 地址解析
  geocoder:function(e){
    let that=this;
    TXMap.geocoder({
      address: e,
      success: function (res) {
        console.log(res);
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
       
      }
    })
  },
  // 用于获取输入关键字的补完与提示，帮助用户快速输入
  getSuggestion:function(e){
    let that=this;
    TXMap.getSuggestion({
      keyword: e,
      success: function (res) {
        console.log(res);
      
        that.setData({
          AddFindList: res.data
        })
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
       
      }
    })
  },
  // 移动地图
  mapMove:function(e){
    let that=this;
    let TXmapM = wx.createMapContext('map');
    TXmapM.getCenterLocation({
      success:function(res){
        that.setData({
          Midlatitude: res.latitude,
          Midlongitude: res.longitude,
          Mlatitude: res.latitude,
          Mlongitude: res.longitude,
        });

        that.reverseGeocoder(res.latitude, res.longitude)
     
      },
      fail:function(e){
        console.log(e);
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // selectListItem
  selectListItem:function(e){
    let that=this;
    let ItemVal = e.currentTarget.dataset.item;
 
    that.setData({
      selectIndexVal:e.currentTarget.dataset.index,
    });       
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
  console.log(ItemVal)
    prevPage.setData({
      // TopImageHide: false,
      // TopImage: e.currentTarget.dataset.url
      addressItem: ItemVal,
      addressDetail: ItemVal.address,
      address: ItemVal.title
    })
    wx.navigateBack();
  },
  // AddressFocus进入输入框
  AddressFocus:function(e){
    let that=this;
    that.setData({
      inputFocusStatus: true,
    })
  },
  // AddressChang输入时
  AddressChange:function(e){
    let addrVal=e.detail.value;
    let that =this;
    that.setData({
      addressVal: addrVal,
    })
 
  },
  // inputCanle取消
  inputCanle:function(){
    let that = this;
    that.setData({
      addressVal: '',
      inputFocusStatus: false,
      AddFindList:[],
      selectIndexVal:-1,
    })
  },
  // inputSearch搜索
  inputSearch:function(e){
    let that=this;
    that.getSuggestion(that.data.addressVal);
  },
  // FocusItemClick 过滤结果搜索
  FocusItemClick:function(e){
    let ItemVal = e.currentTarget.dataset.item;
  
    let that=this;
    that.setData({
      Midlatitude: ItemVal.location.lat,
      Midlongitude: ItemVal.location.lng,
      Mlatitude: ItemVal.location.lat,
      Mlongitude: ItemVal.location.lng,
      AddFindList: [],
      addressVal: '',
      selectIndexVal:-1,
      inputFocusStatus: false,
    });
    let location = ItemVal.location.lat + ',' + ItemVal.location.lng
     that.addFindList(ItemVal.title,location);
  }
 
})